package cn.ttxs.wq.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.haibin.calendarview.Calendar;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.ttxs.lib.base.BaseFragment;
import cn.ttxs.lib.base.WebViewActivity;
import cn.ttxs.lib.tool.ToolLog;
import cn.ttxs.lib.wiget.SlideRecyclerView;
import cn.ttxs.wq.R;
import cn.ttxs.wq.activity.CustomActivity;
import cn.ttxs.wq.activity.MultiActivity;
import cn.ttxs.wq.adapter.SceneWorkAdapter;
import cn.ttxs.wq.dialog.DateSelectDialog;


public class HomeFragment extends BaseFragment {
    public static final String ARGS_PAGE = "args_page";
    private int mPage;
    private ImageView iv_back;
    private SlideRecyclerView slide_recycle;
    private List<String> dataList = new ArrayList<>();
    SceneWorkAdapter mAdapter;

    public static HomeFragment newInstance(int page) {
        ToolLog.w("page", "position:" + page);
        Bundle args = new Bundle();
        args.putInt(ARGS_PAGE, page);
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARGS_PAGE);
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
        iv_back = view.findViewById(R.id.iv_back);
        slide_recycle = view.findViewById(R.id.slide_recycle);
        initData();
        setView();
    }


    private void initData() {
        dataList.add("日历");
        dataList.add("多选日历");
        dataList.add("多选日历弹窗");
        dataList.add("webView 内存泄露测试");
        int start = 4;
        for (int i = start; i < 10 + start; i++) {
            dataList.add("item" + i);
        }
        mAdapter = new SceneWorkAdapter(dataList);
    }

    private void setView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        slide_recycle.setLayoutManager(layoutManager);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                showToast("--点击了" + position);
            }
        });
        mAdapter.addChildClickViewIds(R.id.txt_delete, R.id.ll_item);
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()) {
                    case R.id.txt_delete:
                        // 删除
                        slide_recycle.closeMenu();
                        dataList.remove(position);
                        mAdapter.notifyDataSetChanged();
                        break;
                    case R.id.ll_item:
                        showToast("--点击了" + position);
                        switch (position) {
                            case 0:
                                startActivity(new Intent(getContext(), CustomActivity.class));
                                break;
                            case 1:
                                startActivity(new Intent(getContext(), MultiActivity.class));
                                break;
                            case 2:
                                DateSelectDialog dialog = new DateSelectDialog(getContext());
                                dialog.setOnDateSelectedListener(new DateSelectDialog.OnDateSelectedListener() {
                                    @Override
                                    public void onDateSelected(Map<String, Calendar> dateBeanMap) {
                                        for (Map.Entry<String, Calendar> entry : dateBeanMap.entrySet()) {
                                            ToolLog.w("date", entry.getKey());
                                        }
                                    }
                                });
                                dialog.show();
                                break;
                            case 3:
                                Intent webIntent = new Intent(getContext(), WebViewActivity.class);
                                webIntent.putExtra("url", "https://blog.csdn.net/chennai1101/article/details/103798870");
                                webIntent.putExtra("title", "内存泄露测试");
                                startActivity(webIntent);
                                break;
                        }
                        break;
                }
            }
        });
        slide_recycle.setAdapter(mAdapter);
    }

    @Override
    public void doBusiness(Context mContext) {
    }
}